$(function () {
    var blogId = $("#blogId").val();
    var count_comments = $("#count_comments").text();
    var curr = $("#curr").text();

    layui.use('laypage', function(){
        var laypage = layui.laypage;

        //执行一个laypage实例
        laypage.render({
            elem: 'comment_pages'
            ,layout:['prev', 'page', 'next','skip']
            ,limit:5
            ,count: count_comments //数据总数，从服务端得到
            ,curr:curr
            ,jump: function(obj, first){
                //obj包含了当前分页的所有参数，比如：
                //console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                //console.log(obj.limit); //得到每页显示的条数
                //首次不执行
                if(!first){
                    //do something
                    /*$.ajax({//没有model了
                        type:"get"
                        ,url:'/comment/page?page='+obj.curr+"&limit="+obj.limit+"&blogId="+blogId
                        ,success:function(r) {
                            console.log(r);
                            layer.msg(r);
                        }
                    })*/
                    window.location.href="/comment/page?page="+obj.curr+"&limit="+obj.limit+"&blogId="+blogId;
                }
            }
        });
    });

    $("#comment_insert_btn").click(function () {
        if($("#enable_comment").text()==1){
            layer.msg("本博客暂时不允许评论");
            return;
        }

        var params = $("#comment_form").serialize();

        if(valid_comment()){
            $.ajax({
                type:"post"
                ,url:'/comment/insert'
                ,data:params
                ,success:function (r) {
                    if(r=='success'){
                        layer.msg("评论成功");
                        reset();
                    }else if(r=="no_verify"){
                        layer.msg("验证码错误");
                    }else{
                        layer.msg("评论失败");
                    }
                    $("#verify").attr("src","/common/kaptcha?d="+new Date()*1);
                }
            })
        }
    })






})



function reset() {
    $("#commentator").val('');
    $("#email").val('');
    $("#url").val('');
    $("#verifyCode").val('');
    $("#content").val('');
}


function valid_comment() {
    var commentator = $("#commentator").val();
    var email = $("#email").val();
    var verifyCode = $("#verifyCode").val();
    var content = $("#content").val();
    if(isNull(commentator)){
        layer.msg("请输入名称");
        return false;
    }
    if(isNull(email)){
        layer.msg("请输入邮箱");
        return false;
    }
    if(isNull(verifyCode)){
        layer.msg("请输入验证码");
        return false;
    }
    if(isNull(content)){
        layer.msg("请输入评论");
        return false;
    }
    if(!reg_email(email)){
        layer.msg("请输入正确邮箱格式");
        return false;
    }
    return true;
}